//
//  StatusCellCell.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/11.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit


/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

//MARK: - 自定义 Cell
/// 自定义父类 Cell
class StatusCell: UITableViewCell {
    
    ///单条微博 视图模型
    var viewModel: StatusViewModel? {

        didSet {
            //给子控件赋值
            
            //给顶部视图 视图模型
            topView.viewModel = viewModel
            
            //给微博正文数据
            textView.text = viewModel?.status.text
            
            //给配图视图 视图模型
            picView.viewModel = viewModel
            picView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(picView.bounds.height)
                // 直接设置宽度数值
                make.width.equalTo(picView.bounds.width)
            }
        }
    }
    
    /// 计算行高
    ///
    /// - parameter vm: 视图模型
    ///
    /// - returns:  Cell 行高
    func calculateRowHeight(vm: StatusViewModel) -> CGFloat {
        viewModel = vm
        //强制更新约束
        contentView.layoutIfNeeded()
        //获取cell 最大 Y 值
        let cellMaxY = CGRectGetMaxY(bottomView.frame)
        
        return cellMaxY
    }
    
    /// 构造方法
    ///
    /// - parameter reuseIdentifier: cell 重用标识
    ///
    /// - returns:  tableviewCell
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
         setupUI()
        //取消Cell选中状态
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 懒加载控件
    /// 顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    /// 微博正文
    lazy var textView: UILabel = UILabel(title: "", textSize: 15, screenInset: StatusCellMargin)
    /// 配图视图
    lazy var picView: StatusPictureView = StatusPictureView()
    /// 底部视图
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
}

// MARK: - 设置 UI 界面
extension StatusCell {
    
    func setupUI() {
        
    //1. 添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(textView)
        contentView.addSubview(picView)
        contentView.addSubview(bottomView)
    
    //2. 设置自动布局
/// 顶部视图
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        
/// 微博正文
        textView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
        }
            
/// 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(picView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
        }
    }
}